<?php

/*
	Class: Config

	About: Author
		J William McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
		<App_Controller_Action_Abstract>
*/
class ConfigController extends  App_Controller_Action_Abstract {

	/* Group: Instance Methods */

	/*
		Function: init
			Invoked automatically when an instance is created.
			Initializes the current instance.
			Also initializes the parent object (calls init() on the parent instance).
	*/
	function init(){
    	Zend_Loader::loadClass('Config');
        parent::init();
    }
   
	/* Group: Actions */

	/*
		Function: index

		Plugin Hooks:
			- *config_index_post_save* (action) - Allows you to perform actions after the configuration values are saved to the database. No params.

		View Variables:
			$config - An array of all configurable settings.
			$pagetitle - The HTML title of the page.
			$success - A string containing a message of success to be displayed on the page upon successful configuration update.
	*/
	function indexAction(){
		$config_table = new Config();
        $request = new App_Request($this->getRequest());
        if($this->_request->isPost()){ 
			//we are posting
        	$config_params = $this->_request->getParams();
        	foreach($config_params as $ckey => $value){
				$data = array("value" => $value);
				
				if($request->has("module")){
					$data['module'] = $request->module;
				} else {
					$data['module'] = "default";
				}
				
				$config_table->update($data, "ckey = '" . $ckey . "'");
        	}
			$this->view->success = "Configuration Updated.";			
			$params = array();			
			$this->_app_plugin->doAction($this->_mca . '_post_save', $params); // ACTION HOOK
        }     	
        $config = $config_table->fetchAll();
        if(count($config) > 0){
        	$this->view->config = $config->toArray();  
        }       
	}	
}
